<?php
    
    $stt = MyFunctionCustom::getStatusBooking($model->status);
?>

<div class="page-content">
    <div class="row-fluid">
        <div class="span12">
            <div class="row-fluid">
                <div class="widget-box">
                    <div class="widget-header widget-header-flat">
                        <h4 class="smaller red"><?php echo MyFunctionCustom::getInfoEvent($model->event_id,2); ?></h4>
                        <div class="widget-toolbar">
                            <label>
                                <small class="green">
                                    <?php if($model->status==STATUS_PENDING) : ?>
                                        <b class="red"><?php echo $stt; ?></b>
                                    <?php elseif($model->status == STATUS_DELIVER): ?>
                                        <b class="blue"><?php echo $stt; ?></b>
                                    <?php else: ?>
                                        <b class="green"><?php echo $stt; ?></b>
                                    <?php endif; ?>
                                </small>
                                <span class="lbl"></span>
                            </label>
                        </div>
                    </div>
                    <div class="widget-body">
                        <div class="loading" style="display:none">
                            <div style="width:134px;height:100px;margin:0 auto;padding-top: 60px;">
                                <img src="<?php echo Yii::app()->theme->baseUrl; ?>/frontend/images/ajaxloader.gif">
                            </div>
                        </div>
                        <div class="widget-main">
                            
                        <?php $form=$this->beginWidget('CActiveForm', array(
                               'id'=>'booked-form',
                               'enableAjaxValidation'=>false,
                                'enableClientValidation' => true,            
                                'htmlOptions'=>array(
                                    'class'=>'form-horizontal',
                                ),              
                       )); ?>
                    <div class="control-group">
                        <label class="control-label">Customer Name</label>
                        <div class="controls">
                            <label class="control-label text-left"><b class="green"><?php echo $model->user->first_name . ' ' . $model->user->last_name; ?></b></label>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">Facebook Email</label>
                        <div class="controls">
                            <label class="control-label text-left"><b class="green"><?php echo $model->user->email; ?></b></label>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label"> Contact Email</label>

                        <div class="controls">
                            <?php echo $form->textField($model,'email_contact',array('class'=>'span5')); ?>
                        </div>
                        <?php echo $form->error($model,'email_contact'); ?>
                    </div>
                    <div class="control-group">
                        <label for="focusedInput" class="control-label">Phone</label>
                        <div class="controls">
                            <?php echo $form->textField($model,'phone',array('maxlength'=>10, 'class'=>'span5')); ?>
                        </div>
                            <?php echo $form->error($model,'phone'); ?>
                    </div>
                <!--<fieldset>-->
                    <div class="control-group">
                        <label class="control-label">Ticket Order Number</label>
                        <div class="controls">
                            <label class="control-label text-left"><b class="green"><?php echo $model->ticket_order_number; ?></b></label>
                        </div>
                    </div>   
                    <div class="control-group">
                        <div class="controls">
                            <b class="red">Every seat position will be seperate with comma(,)</b>
                        </div>
                    </div>
                    <div class="control-group">
                            <table class="table table-striped table-bordered table-hover">
                                <thead>
                                    <tr>
                                        <th>Ticket Code</th>
                                        <th>Ticket Description</th>
                                        <th>Quantity</th>
                                        <th width="40">Remain Quantity</th>
                                        <th>Price per ticket</th>
                                        <th>Price of each ticket code</th>
                                        <th>Seat</th>
                                        <th>
                                            <a title="Add new ticket for customer" class="blue add-details" href="#modal-form" role="button" data-toggle="modal" booked="<?php echo $model->id; ?>" rel="<?php echo $model->event_id; ?>">
                                                <i class="icon-plus bigger-130"></i>
                                            </a>
                                        </th>
                                    </tr>
                                    
                                </thead> 
                                <tbody>
                                    <?php foreach($model->booked_details as $value) : ?>
                                    <?php $ticket_code = Ticket::model()->findByPk($value->ticket_id); ?>
                                    <tr>
                                        <td>
                                            <?php echo $ticket_code->ticket_code;?>
                                        </td>
                                        <td><?php echo $ticket_code->description_of_ticket; ?></td>
                                        <td>
                                            <select class="price_details span12" detail="<?php echo $value->id; ?>" booked="<?php echo $model->id; ?>">
                                                <?php for($i = 1; $i <= 10; $i++) : ?>
                                                    <?php if($value->quantity == $i) : ?>
                                                        <option selected="selected" value="<?php echo $value->quantity;  ?>"> <?php echo $value->quantity;  ?></option>
                                                    <?php else : ?>
                                                        <option value="<?php echo $i;  ?>"> <?php echo $i;  ?></option>
                                                    <?php endif;?>                                 
                                                <?php endfor; ?>
                                            </select>
                                            
                                        </td>
                                        <td>
                                            <?php $real_quantity = MyFunctionCustom::remain_quantity(null,$ticket_code->temp_quantity,$ticket_code->remain_quantity); ?>
                                            <?php if ($real_quantity < 10) : ?>
                                                <b class="red">
                                                    <?php echo $real_quantity;  ?>
                                                </b>
                                            <?php else : ?>
                                                <b class="blue"><?php echo $real_quantity; ?></b>
                                            <?php endif; ?>
                                        </td>
                                        <td><b class="red"><?php echo number_format($ticket_code->price); ?> VND / 1 Ticket</b></td>
                                        <td>
                                            <b class="green"><?php echo number_format($value->total_price); ?> VND</b>
                                        </td>
                                        <td>
                                            <?php $seat = BookedDetail::getSeatText($value->seat); ?>
                                            <input class="span8 seat-map" type="text" name="Ticket[seat][<?php echo $value->id ?>]" value="<?php if ($seat != null) {
                                                echo $seat;
                                            }  ?>" />
                                        </td>
                                        <td class="td-actions">
                                            <div class="hidden-phone visible-desktop action-buttons">
                                                <a class="red" href="#" onclick="delete_details(<?php echo $value->id; ?>)" title =" Delete Ticket">
                                                    <i class="icon-trash bigger-130"></i>
                                                </a> 
                                            </div>
                                        </td>
                                    </tr>
                                
                                    <?php endforeach;?>
                                </tbody>
                            
                            </table>
                    </div>
                    <div class="control-group">
                        <label class="control-label">Percentage</label>
                        <div class="controls">
                            <?php echo $form->textField($model,'percentage',array('maxlength'=>10, 'class'=>'span1 percentage')); ?> %
                            
                        </div>
                            <?php echo $form->error($model,'percentage'); ?>
                    </div>
                    <div class="control-group">
                        <label class="control-label">Total Price</label>
                        <div class="controls">
                            <label class="control-label text-left"><b class="red total_price" rel="<?php echo $model->total_price; ?>"><?php echo number_format($model->total_price); ?> VND </b></label>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">Percentage Reason</label>
                        <div class="controls">
                            <?php echo $form->textField($model,'percentage_reason',array('class'=>'span6')); ?>
                            
                        </div>
                            <?php echo $form->error($model,'percentage_reason'); ?>
                    </div>
                    <div class="control-group">
                        <label class="control-label">Booked Date</label>
                        <div class="controls">
                            <label class="control-label text-left"><b class="red"><?php echo date("d-M-Y",strtotime($model->booked_time)); ?></b></label>
                        </div>
                    </div> 
                    <div class="control-group">
                        <label class="control-label">Booked Time</label>
                        <div class="controls">
                            <label class="control-label text-left"><b class="red"><?php echo date("H:i",strtotime($model->booked_time)); ?></b></label>
                        </div>
                    </div>
                    
                    
                    <div class="control-group">
                        <label class="control-label">Payment Type</label>
                        <div class="controls">
                               <?php echo $form->radioButtonlist($model, 'payment_type',array(1=>'COD',2=>'Credit Card',3 => 'ATM') ,array('template'=>'{input}{label}','style' => 'float:left;height:13px;opacity:1;position:absolute;margin-left:-17px;')); ?>
                        </div>
                    </div>

                    
                   
                    <div class="control-group">
                        <label for="focusedInput" class="control-label">Payment Address</label>
                        <div class="controls">
                            <?php echo $form->textArea($model,'payment_address',array('rows'=>3, 'cols'=>50, 'class'=>'span5')); ?>
                        </div>
                            <?php echo $form->error($model,'payment_address'); ?>
                    </div>
                   
                    <div class="control-group">
                        <label for="focusedInput" class="control-label">Deliver Address</label>
                        <div class="controls">
                            <?php echo $form->textArea($model,'deliver_address',array('rows'=>3, 'cols'=>50, 'class'=>'span5')); ?>
                        </div>
                            <?php echo $form->error($model,'deliver_address'); ?>
                    </div>
                    
                    <div class="control-group">
                        <label for="focusedInput" class="control-label">Additional Note</label>
                        <div class="controls">
                            <?php echo $form->textArea($model,'additional_note',array('rows'=>3, 'cols'=>255, 'class'=>'span5')); ?>
                        </div>
                            <?php echo $form->error($model,'additional_note'); ?>
                    </div>
<!--                    <div class="control-group">
                        <label for="focusedInput" class="control-label">Status</label>
                        <div class="controls">
                            <?php echo $form->dropdownlist($model,'status', MyFunctionCustom::$status_booking, array('class'=>'span5')); ?>
                        </div>
                            <?php echo $form->error($model,'status'); ?>
                    </div>-->
                    <div class="control-group">
                        <label for="focusedInput" class="control-label">Seat map</label>
                        <div class="controls">
                            
                                <?php 
                                    $seat_map = Ticket::model()->findByAttributes(array('event_id' => $model->event_id,'seat_map' => 1)); 
                                    // var_dump($seat_map->pic_seat_map);die();
                                    if ($seat_map['pic_seat_map'] != null) :
                                ?>
                                    <img src='<?php echo Yii::app()->baseUrl . "/timthumb.php?src=" . Yii::app()->baseUrl . "/data/pic/" . $seat_map->pic_seat_map . "&w=750"; ?>' />
                                <?php endif; ?>
                            
                        </div>
                    </div>                  
                    <div class="control-group">
                        <div class="controls">
                            <label>
                                <?php if(($model->status == STATUS_DELIVER && $model->payment_type == 1)) : ?>
                                    <input type="checkbox" name="Sendmail" checked="checked" /> <span class="lbl">Send Email From Customer</span>
                                <?php elseif($model->status == STATUS_PENDING && ($model->payment_type == 2 || $model->payment_type == 3)) : ?>
                                    <input type="checkbox" name="Sendmail" checked="checked" /> <span class="lbl">Send Email From Customer</span>
                                    <?php else : ?>
                                    <input type="checkbox" name="Sendmail" /> <span class="lbl">Send Email From Customer</span>
                                <?php endif; ?>
                            </label>
                            
                        </div>
                    </div>
                    <div class="form-actions">
                        <button type="submit" class="btn btn-primary">Save changes</button>
                        <a class="btn" href="<?php echo Yii::app()->createAbsoluteUrl("bookingsystem/booked/index"); ?>">Cancel</a>
                    </div>
                <!--</fieldset>-->
         <?php $this->endWidget(); ?>
                    </div>
        
                </div>
        </div>
    <!--</div>/span-->
    </div><!--/span-->
</div><!--/row-->
<div id="modal-form" class="modal hide" tabindex="-1">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="blue bigger"></h4>
    </div>

    <div class="modal-body overflow-visible">

    </div>
    <div class="modal-footer">
        <button class="btn btn-small" data-dismiss="modal">
            <i class="icon-remove"></i>
            Close
        </button>
    </div>
</div>
<script  type="text/javascript">

    $('.add-details').on('click',function(){
        var event_id = $(this).attr('rel');
        var booked_id = $(this).attr('booked');
        $.ajax({
            url: '<?php echo Yii::app()->createAbsoluteUrl("bookingsystem/booked/add_details"); ?>',
            type: 'POST',
            data: {event_id : event_id, booked_id: booked_id},
            async: true,
            beforeSend: function(){
                $('.modal-body').empty();
                $('.modal-body').append('<div class="loading"><div style="width:134px;height:100px;margin:0 auto;padding-top: 10px;"><img src="<?php echo Yii::app()->theme->baseUrl; ?>/frontend/images/ajaxloader.gif"></div></div>');
            },
            success:function(data){
                $('.modal-body').html(data);
            }
        });
    });
    $('.save-details').bind('click', function(e) {

            e.preventDefault();
            var $this = $(this);
            var remote = $this.data('load-remote');
            var img = '<div class="loading"><div style="width:134px;height:100px;margin:0 auto;padding-top: 60px;"><img src="<?php echo Yii::app()->theme->baseUrl; ?>/frontend/images/ajaxloader.gif"></div></div>';
            $($this.data('remote-target')).html(img);
            href = $this.attr('href');
            $(href).modal('show');
            $.ajax({
                url: remote,
                type: 'POST',
                success: function(data) {
                    $($this.data('remote-target')).html(data);
                    booked();

                }
            });
            return false;
        });
    $('.price_details').on('change',function(){
        $('.widget-main').hide();
        $('.loading').show();
        var book = {};
        book['booked_id'] = $(this).attr('booked');
        book['detail_id'] = $(this).attr('detail');
        book['ticket_quantity'] = $(this).val();
        $.ajax({
            url: '<?php echo Yii::app()->createAbsoluteUrl("bookingsystem/booked/calculate_price_details"); ?>',
            method: 'POST',
            data: {book:book},
            async: true,
            success: function(data){
                window.location.reload();
            },
        });
    });
    $('.percentage').on('keyup keydown',function(e){
        $(this).bind("keydown",function(event){
            if( !(event.keyCode == 8                                // backspace
                || event.keyCode == 46                              // delete
                || event.keyCode == null                      // delete
                || event.keyCode == 9                           // tab
//                            || (event.keyCode == 190 || event.keyCode == 110 )                            // dấu chấm (point) 
                || (event.keyCode >= 35 && event.keyCode <= 40)     // arrow keys/home/end
                || (event.keyCode >= 48 && event.keyCode <= 57)     // numbers on keyboard
                || (event.keyCode >= 96 && event.keyCode <= 105))   // number on keypad
                ) {
                    event.preventDefault();     // Prevent character input
                }
        });
        var num = $(this).val();
        var price = $('.total_price').attr('rel');
        var change_price = (parseInt(price) + (price * num / 100));
        $('.total_price').empty();
        $('.total_price').append(number_format(change_price) + ' VND');
        // alert($(this).val());
    });
    $('.quantity').on('click', function(){
            $(this).unbind("keydown");
            $(this).bind("keydown",function(event){
                if( !(event.keyCode == 8                                // backspace
                    || event.keyCode == 46                              // delete
                    || event.keyCode == null                      // delete
                    || event.keyCode == 9							// tab
//                            || (event.keyCode == 190 || event.keyCode == 110 )							// dấu chấm (point) 
                    || (event.keyCode >= 35 && event.keyCode <= 40)     // arrow keys/home/end
                    || (event.keyCode >= 48 && event.keyCode <= 57)     // numbers on keyboard
                    || (event.keyCode >= 96 && event.keyCode <= 105))   // number on keypad
                    ) {
                        event.preventDefault();     // Prevent character input
                    }
            });
    });
    function delete_details(id)
    {
        if (confirm("Are you wanna delete it???")) {
            $.ajax({
                url: '<?php echo Yii::app()->createAbsoluteUrl("bookingsystem/booked/delete_details"); ?>',
                type: 'POST',
                dataType: 'json',
                data: {id : id},
                beforeSend: function(){
                    $('.widget-box').empty();
                    $('.widget-box').append('<div class="loading"><div style="width:134px;height:100px;margin:0 auto;padding-top: 10px;"><img src="<?php echo Yii::app()->theme->baseUrl; ?>/frontend/images/ajaxloader.gif"></div></div>');
                },
                success: function(){
                    window.location.reload();
                }
            });
        }
        else{
            return false;
        }
        
    }
    function number_format (number, decimals, dec_point, thousands_sep) {
  // From: http://phpjs.org/functions
  // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
  // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +     bugfix by: Michael White (http://getsprink.com)
  // +     bugfix by: Benjamin Lupton
  // +     bugfix by: Allan Jensen (http://www.winternet.no)
  // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
  // +     bugfix by: Howard Yeend
  // +    revised by: Luke Smith (http://lucassmith.name)
  // +     bugfix by: Diogo Resende
  // +     bugfix by: Rival
  // +      input by: Kheang Hok Chin (http://www.distantia.ca/)
  // +   improved by: davook
  // +   improved by: Brett Zamir (http://brett-zamir.me)
  // +      input by: Jay Klehr
  // +   improved by: Brett Zamir (http://brett-zamir.me)
  // +      input by: Amir Habibi (http://www.residence-mixte.com/)
  // +     bugfix by: Brett Zamir (http://brett-zamir.me)
  // +   improved by: Theriault
  // +      input by: Amirouche
  // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // *     example 1: number_format(1234.56);
  // *     returns 1: '1,235'
  // *     example 2: number_format(1234.56, 2, ',', ' ');
  // *     returns 2: '1 234,56'
  // *     example 3: number_format(1234.5678, 2, '.', '');
  // *     returns 3: '1234.57'
  // *     example 4: number_format(67, 2, ',', '.');
  // *     returns 4: '67,00'
  // *     example 5: number_format(1000);
  // *     returns 5: '1,000'
  // *     example 6: number_format(67.311, 2);
  // *     returns 6: '67.31'
  // *     example 7: number_format(1000.55, 1);
  // *     returns 7: '1,000.6'
  // *     example 8: number_format(67000, 5, ',', '.');
  // *     returns 8: '67.000,00000'
  // *     example 9: number_format(0.9, 0);
  // *     returns 9: '1'
  // *    example 10: number_format('1.20', 2);
  // *    returns 10: '1.20'
  // *    example 11: number_format('1.20', 4);
  // *    returns 11: '1.2000'
  // *    example 12: number_format('1.2000', 3);
  // *    returns 12: '1.200'
  // *    example 13: number_format('1 000,50', 2, '.', ' ');
  // *    returns 13: '100 050.00'
  // Strip all characters but numerical ones.
  number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
  var n = !isFinite(+number) ? 0 : +number,
    prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
    sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
    dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
    s = '',
    toFixedFix = function (n, prec) {
      var k = Math.pow(10, prec);
      return '' + Math.round(n * k) / k;
    };
  // Fix for IE parseFloat(0.55).toFixed(0) = 0;
  s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
  if (s[0].length > 3) {
    s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
  }
  if ((s[1] || '').length < prec) {
    s[1] = s[1] || '';
    s[1] += new Array(prec - s[1].length + 1).join('0');
  }
  return s.join(dec);
}     
</script>  
<style type="text/css">
    #Booked_payment_type{
        
    }
</style>          